<?php

include(DOCINCLUDES."members/members.inc");
include(DOCINCLUDES."global_files/tables.inc");
include(DOCINCLUDES."global_files/global_validations.inc");

// get the member id from the session
$partner_id=($_REQUEST["partner_id"])?$_REQUEST["partner_id"]:$_SESSION["gmember_id"];
$company_id=($_REQUEST["company_id"])?$_REQUEST["company_id"]:$_SESSION["gmember_id"];

if($_SESSION["gmember_id"] == 1000)
        {
        header("Location:../users/redirect.php");
        exit;
        }
$file_name="report_customization.php";
//$locale= new cLocale($file_name,$_SESSION["guser_preferences"]["language"]);
if(!$_SESSION["cache1"])
     {
     $_SESSION["cache1"]["page"] = $_SERVER["HTTP_REFERER"];
     }
$form=new cForm();
$dv=new cDataValidation;

if($_GET)
        {
        $customization_id=$_GET["customization_id"];
        }
if($_POST)
	{
	$customization_id=$_POST["customization_id"];
	}
if($_POST || $_GET)
	{
	$db->query="select mr.report_id, mr.report_name from $master_report_table mr JOIN $sys_report_customization_table src ON mr.report_id=src.report_id where src.customization_id=$customization_id";
	$db->runQuery();
	$report_array=$db->returnArrays();
	$report_id=$report_array[0]["report_id"];
	$report_name=$report_array[0]["Report Name"];
	}

if($_POST["save"] || $_POST["activate"])
	{
	//print_r($_POST);exit;
	$customization_id=$_POST["customization_id"];
	$c=count($_POST["filter_id"]);
	for($i=0;$i<$c;$i++)
	 	{
		if($_POST["filter_status"][$i]=="on")
			{
			$filter_status[]=$_POST["filter_status"][$i];
			if($_POST["display_name"][$i])
				$filter_display_name[]=$_POST["display_name"][$i];
			else
				$dv->errors["display_name"][$i]="Enter Display Name";
			if($_POST["display_order"][$i])
				if(is_numeric($_POST["display_order"][$i]))
					$filter_order[]=$_POST["display_order"][$i];
				else
					$filter_order[]=" ";
			
			$filter_id[]=$_POST["filter_id"][$i];
			}
		}
	$c=count($_POST["column_id"]);
	for($i=0;$i<$c;$i++)
		{
		if($_POST["column_status"][$i]=="on")
			{
			$custom_column_id[]=$_POST["column_id"][$i];
			if($_POST["display_column_name"][$i])
				$column_display_name[]=$_POST["display_column_name"][$i]?$_POST["display_column_name"][$i]:" ";
			else
				$dv->errors["display_column_name"][$i]="Enter Display Name";
			
			$column_display_order[]=$_POST["display_column_order"][$i]?$_POST["display_column_order"][$i]:"";
			
			$format_details[]=$_POST["format_details"][$i]?$_POST["format_details"][$i]:"";
			
			$decimal_places[]=$_POST["decimal_places"][$i]?$_POST["decimal_places"][$i]:"";
			
			$display_summary[]=$_POST["display_summary"][$i]?"true":"";
			
			$text_align[]=$_POST["text-align"][$i]?$_POST["text-align"][$i]:"";
			}
		}
	
	if(!$dv->errors)
		{
		$dv->validations["custom_name"]=array("isEmpty"=>"Please Enter Custom Name");
		$dv->validate($_POST);
/*		if($_POST["custom_name"]=="Default" || $_POST["custom_name"]=="default")
			{
			$db->query="update sys_report_customization set is_active ='f';";
			$db->runQuery();
			
			echo "saved";
			exit;
			}*/
		$db->query="select 
				customization_name,title 
				from 
				$sys_report_customization_table 
				where 
				customization_id=$customization_id;";
		$db->runQuery();
		$db_custom_names=$db->returnArrays();
		
		$custom_name=$_POST["custom_name"];
		$report_title=$_POST["report_title"];
		if(count($db_custom_names)!=0)
			{
			foreach($db_custom_names as $key=>$value)
				{
				$db_custom_names1[$key]=$db_custom_names[$key]["customization_name"];
				$db_custom_names1[$key]=$db_custom_names[$key]["title"];
				}
			if((in_array($custom_name,$db_custom_names1)))
				{
				$action="update";
				}
			else
				{
				$action="insert";
				}
			}
			
		
		else
			{
			$action="insert";
			}
		}
	if(!$dv->errors && $action )
		{
		$db->query="begin work;";
		$db->runQuery();
		
		$c=count($filter_display_name);
		
		if($action=="insert")
			{
			$db->query="select 
					nextval('_100350.sys_report_cus_id_seq') 
					as customization_id;";
			$db->runQuery();
			$db_custom_id=$db->returnArrays();
			$customization_id=$db_custom_id[0]["customization_id"];
			$db->query="insert into 
					$sys_report_customization_table
					(customization_id,report_id,customization_name,title) 
					values
					($customization_id,$report_id,'".$custom_name."','".$report_title."');";
			$db->runQuery();
			
			}
		elseif($action=="update")
			{
			$customization_id=$_POST["customization_id"];
			
			 $db->query="update 
					$sys_report_customization_table 
					
					set 
					customization_name='".$custom_name."',title='".$report_title."'
					
					where
					customization_id=$customization_id;";
			$db->runQuery();
			
			$db->query="delete from 
					$sys_filter_customization_table 
					where 
					customization_id=$customization_id";
			$db->runQuery();
			
			$db->query="delete from 
					$report_customization_table 
					where 
					customization_id=$customization_id";
			$db->runQuery();
			}
		for($i=0;$i<$c;$i++)
			{
			$db->query="insert into 
					$sys_filter_customization_table 
					(customization_id,
					filter_id,
					filter_display_name,
					filter_order) 
					
					values 
					($customization_id,
					$filter_id[$i],
					'".$filter_display_name[$i]."',
					$filter_order[$i])";
			$db->runQuery();
			}
		$c=count($custom_column_id);
		for($i=0;$i<$c;$i++)
			{
			if($text_align[$i])
				{
				$field_name="style,";
				$field_value="'$text_align[$i]',";
				}
			if($display_summary[$i])
				{
				$field_name.="display_summary,";
				$field_value.="'$display_summary[$i]',";
				}
			if($decimal_places[$i])
				{
				$field_name.="decimal_places,";
				$field_value.=$decimal_places[$i].",";
				}	
			if($format_details[$i])
				{
				$field_name.="format_details,";
				$field_value.="'$format_details[$i]',";
				}
			if($column_display_order[$i])
				{
				$field_name.="display_order,";
				$field_value.=$column_display_order[$i].",";
				}		
			$db->query="insert into
					$report_customization_table 
					
					(column_id,
					display_name,
					$field_name
					customization_id)
					
					values
					($custom_column_id[$i],
					'$column_display_name[$i]',
					$field_value
					$customization_id);";
					
			$db->runQuery();
			unset($field_name,$field_value);
			}
		
		if($_POST["activate"])
			{
	/*		$db->query="update sys_report_customization set is_active ='f';";
			$db->runQuery();
	*/		$db->query="update
					$sys_report_customization_table
					set
					is_active='t'
					where
					customization_id=$customization_id;";
			$db->runQuery();
			}
		
		if($db->result)
			{
			$db->query="commit work;";
			$db->runQuery();
			//header("location:sales_order_report.php?customization_id=2");
			$previous_page = $_SESSION["cache1"]["page"];
			unset($_SESSION["cache1"]);
			header("Location:$previous_page");
			exit;
			}
		}
	}

$page->title=$locale->LABEL[$file_name]["Title"];
$page->heading="Customizing $report_name";	
$page->doHeaderHtml();
$page->doCentreHtml();

echo "<form name=\"select_action\" method=\"POST\" Actiom=\"$_PHPSELF\">";
	echo "<div align=\"center\">";
/*	$db->query="select 
			customization_id,
			customization_name 
			from 
			$sys_report_customization_table;";
	$db->runQuery();
	$custom_name_array=$db->returnArrays();
			
	$default_array=array("0"=>array("0"=>"0","1"=>"Default"));
	if(count($custom_name_array)!=0)
		$custom_name_array=array_merge($default_array,$custom_name_array);
	else
		$custom_name_array=$default_array;
	echo "<br><br> ".$form->generateInputTag("hidden",array("name"=>"show","value"=>"Show"));
	if(!$_POST)
		{
		$db->query="select customization_id from $sys_report_customization_table where is_active='t' and report_id=$report_id;";
		$db->runQuery();
		$customization_id_array=$db->returnArrays();
		$customization_id=$customization_id_array[0]["customization_id"];
		if($customization_id)
			$_POST["customization_id"]=$customization_id;
		}
	echo "Choose a customization name to edit or create.<br>";
	echo
	$form->generateSelectMenu("customization_id","$_POST[customization_id]",$custom_name_array,"","","--select--","onChange=showDetails(this.options[this.selectedIndex].value);","")."<br>";
	
	echo $form->generateInputTag("hidden",array("name"=>"report_id","value"=>$report_id));
	echo "</form>";*/

if($customization_id)
	{

	///default fetch all filters
	$db->query="select 
			
				filter_id,
				filter_name,
				form_display
				 
				from
				$sys_master_filter_table
				
				where 
				filter_id in 
					(select 
						filter_id 
						from 
						$sys_report_filter_table 
						where 
						report_id in (select report_id from $sys_report_customization_table where customization_id=$customization_id))";
	$db->runQuery();
	$filter_db_array=$db->returnArrays();
	$c=count($filter_db_array);
	if($customization_id!=0)
		{	
		$db->query="select 
				filter_id,
				filter_display_name,
				filter_order 
				
				from 
				$sys_filter_customization_table 
				
				where 
				customization_id=$customization_id;";
	
		$db->runQuery();
		$display_details_array=$db->returnArrays();
				
	//this two loops is to assign thier corresponding custom details to filters.
	
		for($i=0;$i<$c;$i++)
			{
			for($j=0;$j<$c;$j++)
				{
				if($display_details_array[$i]["filter_id"]==$filter_db_array[$j]["filter_id"])
					{
					$_POST["display_name"][$j]=$_POST["display_name"][$j]?$_POST["display_name"][$j]:$display_details_array[$i]["filter_display_name"];
		
					$_POST["display_order"][$j]=$_POST["display_order"][$j]?$_POST["display_order"][$j]:$display_details_array[$i]["filter_order"];
		
					$check_status[$j]="checked";
					}
				
				}
			}
		//query to fetch custom name from sys_report_customization for showing custom name default for this customization id.
		$db->query="select 
				customization_name,title
				 
				from 
				$sys_report_customization_table 
				
				where 
				customization_id=$customization_id;";
		$db->runQuery();
		$db_custom_array=$db->returnArrays();

		
		}
	else
		{
		for($i=0;$i<$c;$i++)
			{
			$_POST["display_name"][$i]=$_POST["display_name"][$i]?$_POST["display_name"][$i]:$filter_db_array[$i]["form_display"];
			
			$_POST["display_order"][$i]=$_POST["display_order"][$i]?$_POST["display_order"][$i]:$i+1;
			
			$check_status[$i]="checked";
			}
		}
		$title_array[$i]["Title"]=
			$form->generateInputTag("text",array("name"=>"display_name[$i]","value"=>$db_custom_array[0]['title'],"size"=>"12"));
	
	$_POST["custom_name"]=$_POST["custom_name"]?$_POST["custom_name"]:($db_custom_array[0]["customization_name"])?$db_custom_array[0]["customization_name"]:"Default";
	$_POST["report_title"]=$_POST["report_title"]?$_POST["report_title"]:($db_custom_array[0]["title"])?$db_custom_array[0]["title"]:"";
	echo "<form name=\"show_details\" method=\"POST\" Actiom=\"$_PHPSELF\">";
	

	for($i=0;$i<$c;$i++)
		{
		$filter_details_array[$i]["Filter Name"]=
			$filter_db_array[$i]["form_display"];
			
		$filter_details_array[$i]["Display Name"]=
			$form->generateInputTag("text",array("name"=>"display_name[$i]","value"=>$_POST["display_name"][$i],"size"=>"12")).$dv->errors["display_name"][$i];
			
		$filter_details_array[$i]["Display Order"]=
			$form->generateInputTag("text",array("name"=>"display_order[$i]","value"=>$_POST["display_order"][$i],"size"=>"8")).$dv->errors["display_order"][$i];
			
		$filter_details_array[$i]["Show(Y/N)"]=
			"<input type=\"checkbox\" name=\"filter_status[$i]\"  $check_status[$i]>";
		
		echo $form->generateInputTag("hidden",array("name"=>"filter_id[$i]","value"=>$filter_db_array[$i][filter_id]));
		
		}
	echo "<h2>
	Filter Customization</h2>";
	doHorizontaltable($filter_details_array,"40%");
	
	echo $form->generateInputTag("hidden",array("name"=>"report_id","value"=>$report_id));	
	echo $form->generateInputTag("hidden",array("name"=>"customization_id","value"=>$customization_id));
	
	
	/////report customization
	
	$alignment_array["Left"]="left";
	$alignment_array["Center"]="center";
	$alignment_array["Right"]="right";
	formatConstraintArray($alignment_array);
	echo $form->generateInputTag("hidden",array("name"=>"report_code","value"=>$report_code));
	echo $form->generateInputTag("hidden",array("name"=>"partner_id","value"=>$partner_id));
	$db->query="select 
				mp.privilege_desc, 
				mcm.permission, 
				mrc.column_id, 
				mrc.display_name, 
				mrc.display_order, 
				mrc.style, 
				mrc.display_summary, 
				mrc.decimal_places, 
				mrc.format_details
				
				
				from 
				$master_column_mapping_table mcm, 
				$master_report_customization_table mrc, 
				$master_privilege_table mp, 
				$sys_report_customization_table src
				
                        	where 
				mcm.report_id=src.report_id and 
				src.customization_id='$customization_id' and 
				mcm.column_id=mrc.column_id and 
				mcm.privilege_id=mp.privilege_id 
				order by mrc.display_name
				;";
	$db->runQuery();
	$report_details_array=$db->returnArrays();
	
	if($customization_id!=0)
		{
		$db->query="select 
				mp.privilege_desc, 
				mcm.permission, 
				rc.column_id, 
				rc.display_name, 
				rc.display_order, 
				rc.style, 
				rc.display_summary, 
				rc.decimal_places, 
				rc.format_details  
				
				from 
				$report_customization_table rc join
				$master_column_mapping_table mcm on mcm.column_id=rc.column_id join
				$master_privilege_table mp on mcm.privilege_id=mp.privilege_id
				
				where 
				rc.customization_id=$customization_id
				
				order by rc.display_name
				;";
		$db->runQuery();
		$report_custom_details_array=$db->returnArrays();
		}

	for($j=0;$j<count($report_details_array);$j++)
        {
	
	$column_id=$report_details_array[$j]["column_id"];
	echo $form->generateInputTag("hidden",array("name"=>"column_id[$j]","value"=>"$column_id"));
	if($customization_id==0)
		{
		$_POST["display_column_name"][$j]	=$report_details_array[$j]["display_name"];
		$_POST["display_column_order"][$j]	=$report_details_array[$j]["display_order"];
		$_POST["display_summary"][$j]	=$report_details_array[$j]["display_summary"];
		$_POST["decimal_places"][$j]	=$report_details_array[$j]["decimal_places"];
		$_POST["format_details"][$j]	=$report_details_array[$j]["format_details"];
		if($report_details_array[$i]["style"])
			{
			$style[$j]=explode(";",$report_details_array[$j]["style"]);
			if($style[$j])
				{
				foreach($style[$j] as $parameter)
					{
					list($name,$value)=explode(":",$parameter);
					$_POST[trim($name)][$j]=trim($value);
					}
				}
			}
		$column_status[$j]="checked";
		}
	else
		{
		$c=count($report_details_array);
		for($i=0;$i<$c;$i++)
			{
			for($k=0;$k<$c;$k++)
				{
				if($report_custom_details_array[$i]["column_id"]==$report_details_array[$k]["column_id"])
					{
					$_POST["display_column_name"][$k]	=$report_custom_details_array[$i]["display_name"];
					
					$_POST["display_column_order"][$k]	=$report_custom_details_array[$i]["display_order"];
					
					$_POST["display_summary"][$k]	=$report_custom_details_array[$i]["display_summary"];
					
					$_POST["decimal_places"][$k]	=$report_custom_details_array[$i]["decimal_places"];
					
					$_POST["format_details"][$k]	=$report_custom_details_array[$i]["format_details"];
					
					$column_status[$k]="checked";
					
					if($report_custom_details_array[$i]["style"])
						{
						$style[$k]=explode(";",$report_custom_details_array[$i]["style"]);
						if($style[$k])
							{
							foreach($style[$k] as $parameter)
								{
								list($name,$value)=explode(":",$parameter);
								$_POST[trim($name)][$k]=trim($value);
								}
							}
						}
					}
				}
			}
		
		$_POST["display_column_name"][$j]=$_POST["display_column_name"][$j]?$_POST["display_column_name"][$j]:$report_details_array[$j]["display_name"];
		
		$_POST["display_column_order"][$j]=$_POST["display_column_order"][$j]?$_POST["display_column_order"][$j]:$report_details_array[$j]["display_order"];
		
		$_POST["display_summary"][$j]=$_POST["display_summary"][$j]?$_POST["display_summary"][$j]:$report_details_array[$j]["display_summary"];
		
		$_POST["decimal_places"][$j]=$_POST["decimal_places"][$j]?$_POST["decimal_places"][$j]:$report_details_array[$j]["decimal_places"];
		
		$_POST["format_details"][$j]=$_POST["format_details"][$j]?$_POST["format_details"][$j]:$report_details_array[$j]["format_details"];	
		
		if($report_details_array[$j]["style"])
			{
			$style[$j]=explode(";",$report_details_array[$j]["style"]);
			if($style[$j])
				{
				foreach($style[$j] as $parameter)
					{
					list($name,$value)=explode(":",$parameter);
					$_POST[trim($name)][$j]=$_POST[trim($name)][$j]?$_POST[trim($name)][$j]:trim($value);
					}
				}
			}
		}
		
		
		
	if($_POST["display_column_order"][$j] <= 0)
		{
		unset($_POST["display_column_order"][$j]);
		}
	

	$column_mapping_details[$j]["Column Name"]=
		$report_details_array[$j]["display_name"];
		
	$column_mapping_details[$j]["Display Name"]=
		$form->generateInputTag("text",array("name"=>"display_column_name[$j]","value"=>"{$_POST[display_column_name][$j]}","maxlength"=>"40","size"=>"25")).$errors[$j]["display_column_name"];
		
	$column_mapping_details[$j]["Display Order"]=
		$form->generateInputTag("text",array("name"=>"display_column_order[$j]","value"=>"{$_POST[display_column_order][$j]}","size"=>"10", "maxlength"=>"4")).$errors[$j]["display_column_order"];
		
	$column_mapping_details[$j]["Show(Y/N)"]=
		"<input type=\"checkbox\" name=\"column_status[$j]\" id=\"column_status_id[$j]\"  $column_status[$j]>";
		
	$column_mapping_details[$j]["Alignment"]=
		$form->generateSelectMenu("text-align[$j]",$_POST["text-align"][$j],$alignment_array,"","","yes","","narrow").$errors[$j]["text-align"];
		
	$column_mapping_details[$j]["Format Details"]=
		$form->generateInputTag("text",array("name"=>"format_details[$j]","value"=>"{$_POST[format_details][$j]}","size"=>"15", "maxlength"=>"255")).$errors[$j]["format_details"];
		
	if($report_details_array[$j]["decimal_places"])
		{
		$column_mapping_details[$j]["Decimal Places"]=
			$form->generateInputTag("text",array("name"=>"decimal_places[$j]","value"=>"{$_POST[decimal_places][$j]}","size"=>"11","maxlength"=>"1")).$errors[$j]["decimal_places"];
		}
	else
		{
		$column_mapping_details[$j]["Decimal Places"]="";
		}

	if($report_details_array[$j]["display_summary"])
		{
		$check_status=($_POST["display_summary"][$j]=='t')?"checked":"";
		
		$column_mapping_details[$j]["Display Summary"]=
			"<input type=\"checkbox\" name=\"display_summary[$j]\" value=\"t\" $check_status>";
		}
	else
		{
		$column_mapping_details[$j]["Display Summary"]="";
		}
        }
	setNA($column_mapping_details);
	
	echo "<h2>
	Report Customization</h2>";/*$title_array*/
doHorizontalTable($column_mapping_details,"");
// 	doHorizontalTable($title_array,"");        
	echo $form->generateInputTag("button",array("name"=>"reset_order","value"=>"Clear Order","onClick"=>"resetSelection($c);"));
	$vertical_table_array[0]["Customization Name :"]=
	$form->generateInputTag("text",array("name"=>"custom_name","value"=>$_POST["custom_name"],"size"=>"12")).$dv->errors["custom_name"];
	$vertical_table_array[0]["Report Title :"]=
	$form->generateInputTag("text",array("name"=>"report_title","value"=>$_POST["report_title"],"size"=>"50"));
	doVerticaltable($vertical_table_array,"70%");
	echo "<br> ".$form->generateInputTag("submit",array("name"=>"save","value"=>"Save"));
	echo "<lt> ".$form->generateInputTag("submit",array("name"=>"activate","value"=>"Save & Activate"));
	echo "</form>";	
	}
	$page->doFooterHtml();
?>
<script language="Javascript">
function showDetails(target)
	{
	document.select_action.submit();
	} 
function getAction()
	{
	var key=(confirm('This customize Name already exist.\n Do you want to overwrite it?'));
	if(key=="true")
		{
		document.show_details.submit();
		}
	}
function resetOrder(target)
	{
	for(i=0;i<target;i++)
		{
		document.show_details.elements["display_column_order["+i+"]"].value="";
		}
	}
function resetSelection(target)
	{
	for(i=0;i<target;i++)
		{
		document.getElementById("column_status_id["+i+"]").checked=false;
		}
	}
function checkAll(field)
	{
	for (i = 0; i < field.length; i++)
		field[i].checked = true ;
	}

function uncheckAll(field)
	{
	for (i = 0; i < field.length; i++)
		field[i].checked = false ;
	}
function Toggle(item)
	{
	obj=document.getElementById(item);
	visible=(obj.style.display!="none")
	if (visible)
		{
		obj.style.display="none";
   		}
	 else
		{
      		obj.style.display="block";
		}
	}

</script>
